package com.sk.bilibili.service;

import com.sk.bilibili.dto.SlideCode;
import com.sk.bilibili.other.R;
import com.sk.bilibili.property.SlideCodeProperty;
import com.sk.bilibili.utils.BSessionUtils;
import com.sk.bilibili.utils.SlideCodeUtils;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 邵康
 * @date 2021/11/22 12:30
 */
@Log
@Service
public class SlideCodeService {

    @Autowired
    private SlideCodeUtils slideCodeUtils;
    @Autowired
    private SlideCodeProperty slideCodeProperty;

    public R<SlideCode> generateSlideCode() {
        SlideCode slideCode = slideCodeUtils.generateSlideCode();
        BSessionUtils.setSlideCodeOffset(slideCode.getOffset());
        slideCode.setOffset(null);
        return R.success(slideCode);
    }

    public R<Void> checkSlideCode(Integer offset) {
        Integer save = BSessionUtils.getSlideCodeOffset();
        if(save==null)return R.error("验证码过期");
        return R.res(Math.abs(offset-save)<slideCodeProperty.getOffset());
    }

}
